import api from '@/api/dictionary'

const state = {
    dic: null,
    dicObj: {},
    dicArray: [],
}

const actions = {
    async loadDic({ commit, state }) {
        //加载字典数据
        let ret = await api.index()
        //将字典解析成对象，便于将id转成中文
        let dicObj = {}
        let dicArray = {}

        ret.data.forEach(data => {
            dicObj[data.id] = data.name
            if (!dicArray[data.type]) {
                dicArray[data.type] = []
            }
            dicArray[data.type].push({
                name: data.name,
                id: data.id,
                value: data.value
            })
        })
        commit('setDic', { dicObj, dicArray })
    }
}

const mutations = {
    setDic(state, { dic, dicObj, dicArray }) {
        state.dic = dic
        state.dicObj = dicObj
        state.dicArray = dicArray
    },
}


export default {
    state,
    actions,
    mutations
}